package com.financial.transactionhistory.controller;

import com.financial.common.core.domain.R;
import com.financial.common.utils.SecurityUtils;
import com.financial.transactionhistory.dto.FinancialTransactionAddDto;
import com.financial.transactionhistory.dto.FinancialTransactionListDto;
import com.financial.transactionhistory.vo.FinancialTransactionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.financial.common.annotation.Log;
import com.financial.common.core.controller.BaseController;
import com.financial.common.core.domain.AjaxResult;
import com.financial.common.enums.BusinessType;
import com.financial.transactionhistory.domain.FinancialTransaction;
import com.financial.transactionhistory.service.IFinancialTransactionService;
import com.financial.common.utils.poi.ExcelUtil;
import com.financial.common.core.page.TableDataInfo;

/**
 * 交易记录Controller
 *
 * @author chenda
 * @date 2025-06-15
 */
@RestController
@RequestMapping("/financial/Transactionhistory")
@Api(tags =  "交易记录相关接口")
public class FinancialTransactionController extends BaseController
{
    @Autowired
    private IFinancialTransactionService financialTransactionService;

    /**
     * 查询交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('financial:Transactionhistory:list')")
    @GetMapping("/list")
    @ApiOperation("查询交易记录列表")
    public TableDataInfo<List<FinancialTransaction>> list(FinancialTransactionListDto dto)
    {
        startPage();
        List<FinancialTransactionVo> list = financialTransactionService.selectFinancialTransactionList(dto);
        return getDataTable(list);
    }

    /**
     * 导出交易记录列表
     */
    @PreAuthorize("@ss.hasPermi('financial:Transactionhistory:export')")
    @Log(title = "交易记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出交易记录列表")
    public void export(HttpServletResponse response, FinancialTransactionListDto dto)
    {
        List<FinancialTransactionVo> list = financialTransactionService.selectFinancialTransactionList(dto);
        ArrayList<FinancialTransaction> financialTransactions = new ArrayList<>();
        list.forEach(x->{
            FinancialTransaction financialTransaction = new FinancialTransaction();
            BeanUtils.copyProperties(x,financialTransaction);
        });
        ExcelUtil<FinancialTransaction> util = new ExcelUtil<FinancialTransaction>(FinancialTransaction.class);
        util.exportExcel(response, financialTransactions, "交易记录数据");
    }

    /**
     * 获取交易记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('financial:Transactionhistory:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取交易记录详细信息")
    public R<FinancialTransactionVo> getInfo(@ApiParam(value = "交易记录ID", required = true)
                                           @PathVariable("id") Long id)
    {
        return R.ok(financialTransactionService.selectFinancialTransactionById(id));
    }

    /**
     * 新增交易记录
     */
    @PreAuthorize("@ss.hasPermi('financial:Transactionhistory:add')")
    @Log(title = "交易记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增交易记录")
    public AjaxResult add(@ApiParam(value = "交易记录实体", required = true) @RequestBody FinancialTransactionAddDto dto)
    {
        return toAjax(financialTransactionService.insertFinancialTransaction(dto));
    }

    /**
     * 修改交易记录
     */
    @PreAuthorize("@ss.hasPermi('financial:Transactionhistory:edit')")
    @Log(title = "交易记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改交易记录")
    public AjaxResult edit(@ApiParam(value = "交易记录实体", required = true)  @RequestBody FinancialTransactionAddDto dto)
    {
        return toAjax(financialTransactionService.updateFinancialTransaction(dto));
    }

    /**
     * 删除交易记录
     */
    @PreAuthorize("@ss.hasPermi('financial:Transactionhistory:remove')")
    @Log(title = "交易记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除交易记录")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(financialTransactionService.deleteFinancialTransactionByIds(ids));
    }
}